package yl.hs.bmipfull.viewmodels;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import yl.hs.bmipfull.security.TokenUtils;
import yl.hs.bmipfull.utils.ResultCode;

/**
 * @author jingguangxie
 * @category Web接口响应数据
 */
@ApiModel(value = "Web接口数据", description = "接口数据操作响应结果")
public class WebResult {
    /**
     * 状态代码
     */
    private int code;

    /**
     * 响应消息
     */
    private String message;

    /**
     * 响应数据
     */
    private Object data;
    /**
     * token
     */
    private String token;

    public WebResult(Integer code) {
        this.code = code;
        switch (code) {
            case 401 -> this.message = "需要登录";
            case 403 -> this.message = "需要授权";
            case 404 -> this.message = "没有找到资源";
            default -> this.message = "服务器错误";
        }
        this.token = TokenUtils.refreshToken();
    }

    public WebResult(int code, String msg, Object data) {
        this.setCode(code);
        this.setMessage(msg);
        this.setData(data);
        this.token = TokenUtils.refreshToken();
    }

    public WebResult(ResultCode result, String message, Object data) {
        this.setCode(result.code);
        this.setMessage(message);
        this.setData(data);
        this.token = TokenUtils.refreshToken();
    }
    public WebResult(ResultCode result, String message, Object data,String token) {
        this.setCode(result.code);
        this.setMessage(message);
        this.setData(data);
        this.token = token;
    }

    public WebResult(ResultCode result, String message, Object data, boolean isRefresh) {
        this.setCode(result.code);
        this.setMessage(message);
        if (!isRefresh) {
            this.setData(null);
            this.token = data.toString();
        } else {
            this.token = TokenUtils.refreshToken();
        }
    }
    public WebResult(ResultCode result, String message,Object data,Object token, boolean isRefresh) {
        this.setCode(result.code);
        this.setMessage(message);
        if (!isRefresh) {
            this.setData(data);
            this.token = token.toString();
        } else {
            this.token = TokenUtils.refreshToken();
        }
    }

    public WebResult(ResultCode result, String message) {
        this.setCode(result.code);
        this.setMessage(message);
        this.setData(null);
    }

    public WebResult(ResultCode result, boolean isOutTime, String message) {
        this.setCode(result.code);
        this.setMessage(message);
        this.setData(null);
        if (!isOutTime) {
            this.token = TokenUtils.refreshToken();
        } else {
            this.token = "";
        }
    }

    public String getToken() {
        return token;
    }

    public void setToken(String token) {
        this.token = token;
    }

    @ApiModelProperty(name = "code", value = "执行结果状态代码")
    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    @ApiModelProperty(name = "message", value = "执行结果描述信息")
    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    @ApiModelProperty(name = "data", value = "执行结果关联数据")
    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }
}
